package com.eduhzy.ttw.teacher.mvp.presenter;

import android.app.Application;
import android.os.Handler;
import android.os.Message;

import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.teacher.mvp.contract.UpdateClassContract;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.EventBus;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * @author xubo
 * @date 2018-9-26 12:02:28
 */
@ActivityScope
public class UpdateClassPresenter extends BasePresenter<UpdateClassContract.Model, UpdateClassContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public UpdateClassPresenter(UpdateClassContract.Model model, UpdateClassContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void updateClassInfo(String id, String bjmc, String njmc) {
        QMUITipDialog dialog = RxUtil.showLoading(mRootView.getActivity(), "正在修改···");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("id", id);
        map.put("bjmc", bjmc);
        map.put("nj", njmc);
        mModel.updateClassInfo(map)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    dialog.show();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    dialog.dismiss();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<Map>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<Map> commonData) {
                        if (commonData.isResult()) {
                            Message msg = Message.obtain();
                            msg.what = EventBusTags.UPDATE_CLASS_SUCCESS;
                            msg.obj = bjmc;
                            EventBus.getDefault().post(msg,RouterHub.APP_CLASSDETAILSACTIVITY);
                            EventBus.getDefault().post(msg,RouterHub.APP_MINECLASSACTIVITY);
                            mRootView.showMessage("修改成功");
                            mRootView.killMyself();
                        }
                    }
                });
    }
}
